package org.aspectsense.middleware.gui;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeListener implements TreeSelectionListener {

	JTree tree;
	JEditorPane textarea;
	JButton showTree;
	ITreeNode activeNode;
	MainFrame mf;
	public TreeListener (JTree tree, JEditorPane textarea, JButton showTree, MainFrame mainFrame){
		this.tree=tree;
		this.textarea=textarea;
		this.showTree = showTree;
		this.activeNode = null;
		this.mf = mainFrame;
	}
	@Override
	public void valueChanged(TreeSelectionEvent arg0) {
		//Returns the last path element of the selection.
		//This method is useful only when the selection model allows a single selection.
		    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

		    if (node == null)
		    //Nothing is selected.	
		    return;

		    ITreeNode treenode = (ITreeNode) node.getUserObject();
		    mf.setActiveNode (treenode);
		    if (this.textarea!=null)
		    	textarea.setText(treenode.getInfo());
		    
		    if (treenode.isEndUser())
		    {
		    	showTree.setEnabled(true);
		    
		    }
		    else
		    	showTree.setEnabled(false);
		  
	}
	


}
